from typing import *


class ListNode:

    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:

    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        start = ListNode()
        tail , p =start, head
        while p:
            cnt = 0
            nxt = p.next
            while nxt and nxt.val == p.val:
                cnt +=1
                nxt = nxt.next
            if cnt == 0:
                tail.next = p
                tail = tail.next
                tail.next = None
            p = nxt
        return start.next

class Solution:

    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        start = ListNode(0,head)
        p = start
        while p.next and p.next.next:
            if p.next.val == p.next.next.val:
                while p.next.next and p.next.next.val == p.next.val:
                    p.next.next = p.next.next.next
                p.next = p.next.next
            else:
                p = p.next
        return start.next